package com.rf.richfitwheel.admin.sys.controller;

import com.rf.richfitwheel.admin.annotation.UserOperateLog;
import com.rf.richfitwheel.admin.contants.UrlConstant;
import com.rf.richfitwheel.admin.sys.model.SysCertInfo;
import com.rf.richfitwheel.admin.sys.service.SysCertInfoService;
import com.rf.richfitwheel.common.exception.BusinessException;
import com.rf.richfitwheel.common.utils.PageUtils;
import com.rf.richfitwheel.common.utils.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author hushouquan
 */
@RestController
@RequestMapping(UrlConstant.WEB_ADMIN_URL+"/cerInfo")
public class SysCertInfoController{
    private static Logger log = LoggerFactory.getLogger(SysCertInfoController.class);
    
    @Autowired
    private SysCertInfoService sysCertInfoService;
    
    @UserOperateLog(module="系统功能",methods="saveOrUpdate",description="新增或修改证书信息")
    @PostMapping("/saveOrUpdate")
    public R saveOrUpdate(@RequestBody SysCertInfo sysCertInfo){
        try {
            sysCertInfoService.saveOrUpdate(sysCertInfo);
            return R.ok();
        } catch (BusinessException e) {
            log.error(e.getMessage(), e);
            return R.error(e.getMessage());
        }catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error("系统错误");
        }
    }
    
    /**
     * 验证ukey合法性（有效期、状态、用户匹配等）
     * @param certInfo ukey信息
     * @return 结果
     */
    @PostMapping("/checkUkeyAccess")
    public R checkUkeyAccess(@RequestBody Map<String, Object> certInfo){
        try {
            /*[
                "0",
                "XASJUKey",
                "769",
                "82FCD03976363832",
                "SKF_APP",
                "1694157421439",
                "C=CN, O=AVIC SM2, CN=AVIC_SK_SM2_CA_TEST_002",
                "C=CN, O=AVIC SM2, CN=AVIC_SK_SM2_CA",
                "2023-09-08 15:17:03",
                "2024-09-08 15:17:03",
                "04F9003EB38A47D2176148BC10EF00A0",
                "AT_SIGNATURE"
            ]*/
            boolean success = sysCertInfoService.checkUkeyAccess(certInfo);
            if(!success){
                return R.error("Ukey校验失败");
            }
            return R.ok();
        } catch (IllegalArgumentException | BusinessException e) {
            log.error(e.getMessage(), e);
            return R.error(e.getMessage());
        }catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error("系统错误,无法验证ukey有效性");
        }
    }
    
    /**
     * 查询ukey信息
     * @param params {username:,staffName:,certSn:,certStatus:,receStatus:}
     * @return 分页数据
     */
    @GetMapping("/querySysCertInfoPage")
    public R querySysCertInfoPage(@RequestParam Map<String, Object> params) {
        PageUtils page = sysCertInfoService.querySysCertInfoPage(params);
        return R.ok().put("page", page);
    }
    
    /**
     * 开通支付权限
     * @param certInfo 记录
     * @return 开通结果
     */
    @PostMapping("/setPaymentApply")
    public R setPaymentApply(@RequestBody Map<String, Object>[] certInfo){
        try {
//            List<String> certSnList = Arrays.stream(certInfo).map(m -> m.get("cert_sn").toString()).collect(Collectors.toList());
            sysCertInfoService.setPaymentApply(certInfo);
            return R.ok();
        } catch (IllegalArgumentException | BusinessException e) {
            log.error(e.getMessage(), e);
            return R.error(e.getMessage());
        }catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error("系统错误,开通支付权限失败");
        }
    }
    
    /**
     * 关闭支付权限
     * @param certInfo 记录
     * @return 结果
     */
    @PostMapping("/setPaymentCancel")
    public R setPaymentCancel(@RequestBody Map<String, Object>[] certInfo){
        try {
            List<String> idList = Arrays.stream(certInfo).map(m -> m.get("id").toString()).collect(Collectors.toList());
            sysCertInfoService.setPaymentCancel(idList);
            return R.ok();
        } catch (IllegalArgumentException | BusinessException e) {
            log.error(e.getMessage(), e);
            return R.error(e.getMessage());
        }catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error("系统错误,关闭支付权限失败");
        }
    }
    
    /**
     * 审批支付权限
     * @param certInfo 记录
     * @return 结果
     */
    @PostMapping("/paymentApplyDone")
    public R paymentApplyDone(@RequestBody Map<String, Object>[] certInfo){
        try {
            List<String> idList = Arrays.stream(certInfo).map(m -> m.get("id").toString()).collect(Collectors.toList());
            sysCertInfoService.paymentApplyDone(idList);
            return R.ok();
        } catch (IllegalArgumentException | BusinessException e) {
            log.error(e.getMessage(), e);
            return R.error(e.getMessage());
        }catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error("系统错误,关闭支付权限失败");
        }
    }
}
